package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Transactional
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){

        Long userId = BaseContext.getCurrentId();

        //1、查询购物车
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(queryWrapper);


        BigDecimal amount = new BigDecimal("0");//总金额

        List<OrderDetail> orderDetails = new ArrayList<>();//订单详情对象

        //计算总金额
        shoppingCarts.forEach(shoppingCart -> {
            // + (amount * number )
            amount.add(shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber())));

            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(shoppingCart,orderDetail,"id");//id属性不能拷贝
            orderDetails.add(orderDetail);
        });

        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());

        User user = userService.getById(userId);

        //给orders对象填充数据
        orders.setNumber(UUID.randomUUID().toString());//订单号
        orders.setStatus(2);//状态
        orders.setUserId(userId);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setAmount(amount);//总金额
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setConsignee(addressBook.getConsignee());
        orders.setUserName(user.getName());

        //2、清空购物车
        shoppingCartService.remove(queryWrapper);

        //3、添加订单信息
        ordersService.save(orders);
        //才会有orderId
        orderDetails.forEach(orderDetail -> {
            orderDetail.setOrderId(orders.getId());
        });

        //4、添加订单详情信息
        orderDetailService.saveBatch(orderDetails);

        return R.success("下单成功");
    }
}
